// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin_JXGM ql1746365080@gmail.com
 * @Date: 2024-10-16 09:35:57
 * @LastEditors: qiulin ql1746365080@gmail.com
 * @LastEditTime: 2024-12-12 16:01:21
 * @FilePath: \SGFEM\Import\BDFImport\BulkData\Element\CBARParser.cpp
 */
#include "Import/BDFImport/BulkData/Element/CBARParser.h"

#include <cstdint>
#include <iostream>

#include "boost/algorithm/string.hpp"

#include "Utility/Logging/SGLogger.h"

#include "DataStructure/Input/Element/CBARData.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Element/IElementService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (CBAR);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;
/*
// file://./../../Doc/MarkDown/BulkData/CBAR/CBAR.md>
*/
// 以下需要修改
enum CBARkey : std::uint8_t
{
    CBARKEY_EID = 2,
    CBARKEY_PID = 3,  // Property identification number of a PBAR, PBARL or PBRSECT entry.(Integer > 0 or blank*; Default = EID unless BAROR
                      // entry has nonzero entry in field 3.)
    CBARKEY_GA = 4,   // Grid point identification numbers of connection points. (Integer > 0;  )
    CBARKEY_GB = 5,
    CBARKEY_X1 = 6,   // Components of orientation vector , from GA, in the displacement coordinatesystem at GA (Default), or in the basic
                      // coordinate system. See Remark  8. (Real)
    CBARKEY_X2   = 7,
    CBARKEY_X3   = 8,
    CBARKEY_OFFT = 9,   // Offset vector interpretation flag. (character or blank) See Remark 8.
    CBARKEY_PA   = 12,  // Pin flags for bar ends A and B, respectively. Used to remove connections between
                        // the grid point and selected degrees-of-freedom of the bar. The degrees-of-
                        // freedom are defined in the element’s coordinate system (see  Figure 9-11 ). The bar
                        // must have stiffness associated with the PA and PB degrees-of-freedom to be
                        // released by the pin flags. For example, if PA = 4 is specified, the PBAR entry
                        // must have a value for J, the torsional stiffness. (Up to 5 of the unique Integers 1
                        // through 6 anywhere in the field with no embedded blanks; Integer > 0.) Pin flags
                        // combined with offsets are not allowed for SOL 600. Pin flags are not allowed in
                        // SOL 700.
    CBARKEY_PB  = 13,
    CBARKEY_W1A = 14,   // Components of offset vectors  and  , respectively (see  Figure 9-11 ) in
                        // displacement coordinate systems (or in element system depending upon the
                        // content of the OFFT field), at points GA and GB, respectively. See Remark 7.
                        // and  8. (Real; Default = 0.0) Offsets are not allowed in SOL 700.
    CBARKEY_W2A = 15,
    CBARKEY_W3A = 16,
    CBARKEY_W1B = 17,
    CBARKEY_W2B = 18,
    CBARKEY_W3B = 19,
};

SG::DataStructure::Common::Status BDF::BULKDATA::ParseCBAR (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                            std::vector<std::string>&                               dividedEntryInfo)
{
    SG::DataStructure::FEM::CBARData tempCBAR;  // 创建一个临时CBAR数据对象

    // TODO(bug 001) m_dominId的正确填写
    tempCBAR.m_domainId = 1;                 // 暂时将m_domainId设为1，待修复

    int  curIDKey = 1;                       // 当前词条的索引，初始值为1
    bool isVector = false;                   // 用于判断X1、X2、X3是作为向量还是网格点编号
    for (auto curStrKey : dividedEntryInfo)  // 遍历分割后的词条信息
    {
        bool const isEmpty = is_all_space (curStrKey);

        if (isEmpty)  // 如果当前词条为空，则跳过，处理下一个词条
        {
            curIDKey++;
            continue;
        }

        bool const isFloat = covertToScientificNotation (curStrKey);  // 检查当前词条是否为浮点数

        switch (curIDKey)                                             // 根据当前的词条索引，解析对应的数据
        {
        case CBARKEY_EID:
            tempCBAR.m_id = convertToI64 (curStrKey);                 // 解析EID并存储到tempCBAR的m_id
            break;
        case CBARKEY_PID:
            tempCBAR.m_pId = convertToI64 (curStrKey);                // 解析PID并存储到tempCBAR的m_pId
            break;
        case CBARKEY_GA:
            tempCBAR.m_g[0] = convertToI64 (curStrKey);               // 解析GA网格点编号并存储到tempCBAR的m_g[0]
            break;
        case CBARKEY_GB:
            tempCBAR.m_g[1] = convertToI64 (curStrKey);               // 解析GB网格点编号并存储到tempCBAR的m_g[1]
            break;
        case CBARKEY_X1:
            if (!isFloat)                                             // 如果X1不是浮点数，则视为网格点编号
            {
                tempCBAR.m_go = convertToI64 (curStrKey);             // 存储网格点编号到tempCBAR的m_go
            }
            else                                                      // 如果X1是浮点数，则视为向量分量
            {
                tempCBAR.m_x1 = convertToReal (curStrKey);            // 存储X1向量分量到tempCBAR的m_x1
                isVector      = true;                                 // 设置isVector为true，表示后续X2、X3也是向量分量
            }
            break;
        case CBARKEY_X2:
            if (isVector)                                   // 如果X1为向量，则处理X2向量分量
            {
                tempCBAR.m_x2 = convertToReal (curStrKey);  // 存储X2向量分量到tempCBAR的m_x2
            }
            break;
        case CBARKEY_X3:
            if (isVector)                                   // 如果X1为向量，则处理X3向量分量
            {
                tempCBAR.m_x3 = convertToReal (curStrKey);  // 存储X3向量分量到tempCBAR的m_x3
            }
            break;
        case CBARKEY_OFFT:
            // 去除首尾空格
            boost::trim (curStrKey);                                         // 去除当前词条字符串的首尾空格
            tempCBAR.m_flag = SG::DataStructure::FEM::calcFlag (curStrKey);  // 计算OFFT标志并存储到tempCBAR的m_flag
            break;
        case CBARKEY_PA:
            tempCBAR.m_pa = convertToI64 (curStrKey);                        // 解析并存储PA销钉标志
            break;
        case CBARKEY_PB:
            tempCBAR.m_pb = convertToI64 (curStrKey);                        // 解析并存储PB销钉标志
            break;
        case CBARKEY_W1A:
            tempCBAR.m_w1a = convertToReal (curStrKey);                      // 解析并存储W1A偏移分量
            break;
        case CBARKEY_W2A:
            tempCBAR.m_w2a = convertToReal (curStrKey);                      // 解析并存储W2A偏移分量
            break;
        case CBARKEY_W3A:
            tempCBAR.m_w3a = convertToReal (curStrKey);                      // 解析并存储W3A偏移分量
            break;
        case CBARKEY_W1B:
            tempCBAR.m_w1b = convertToReal (curStrKey);                      // 解析并存储W1B偏移分量
            break;
        case CBARKEY_W2B:
            tempCBAR.m_w2b = convertToReal (curStrKey);                      // 解析并存储W2B偏移分量
            break;
        case CBARKEY_W3B:
            tempCBAR.m_w3b = convertToReal (curStrKey);                      // 解析并存储W3B偏移分量
            break;
        default:
            break;
        }
        curIDKey++;  // 处理完当前词条后，索引加1
    }

    // 将解析后的CBAR数据添加到数据库中
    return dbServiceFactory->get<IElementService> ()->Add (tempCBAR);
}